package com.example.match.mui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.match.R;
import com.example.match.adapter.FileAdapter;
import com.example.mylibrary.activity.BaseActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by fairplay on 2018/5/29.
 */

public class FilePickActivity2 extends BaseActivity {

    @Bind(R.id.lv_pick_list)
    ListView lvPickList;
    @Bind(R.id.tv_no_project)
    TextView tvNoProject;

    private FilePickActivity2 mActivity;
    private List<File> mList=new ArrayList<>();

    private FileAdapter fileAdapter;
    private String currentPath;
    private String rootPath;
    private File currentFile;//当前选中的文件

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_pick2);
        mActivity=this;
        ButterKnife.bind(this);
        initObject();
        initView();
        initData();
        setListener();


    }

    @Override
    protected void initData() {
        rootPath= Environment.getExternalStorageDirectory().getAbsolutePath();
        updateFile(rootPath);
    }

    @Override
    protected void initView() {
        fileAdapter=new FileAdapter(mActivity,mList);
        lvPickList.setAdapter(fileAdapter);
    }



    @Override
    protected void initObject() {

    }

    @Override
    protected void setListener() {
        lvPickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                currentFile=mList.get(i);
                if (mList.get(i).isDirectory()){
                    updateFile(mList.get(i).getAbsolutePath());
                }else {
                    Intent intent=new Intent();
                    intent.putExtra("filePath",mList.get(i).getAbsolutePath());
                    setResult(0,intent);
                    finish();
                }
            }
        });
    }

    private void updateFile(String path){
        currentPath=path;
        File file=new File(path);
        if(file.exists()){
            if(file.isDirectory()){
                mList.clear();
                File [] files=file.listFiles();
                if(files.length>0){
                    for (int i = 0; i <files.length ; i++) {
                     if(!files[i].isHidden()){
                        mList.add(files[i]);
                        }
                    }
                    lvPickList.setVisibility(View.VISIBLE);
                    tvNoProject.setVisibility(View.INVISIBLE);
                }else {
                    lvPickList.setVisibility(View.INVISIBLE);
                    tvNoProject.setVisibility(View.VISIBLE);
                }
            }else {
                mList.clear();
                mList.add(file);

            }
            fileAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onBackPressed() {
        if(currentPath.equals(rootPath)){
            Intent intent=new Intent();
            intent.putExtra("filePath","");
            setResult(0,intent);
            finish();
        }else {
            String parentPath=new File(currentPath).getParent();
            updateFile(parentPath);
        }
    }
}
